package com.yyl.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyl.common.pojo.EasyUIDataGridResult;
import com.yyl.common.pojo.TaotaoResult;
import com.yyl.common.utils.IDUtils;
import com.yyl.mapper.TbItemMapper;
import com.yyl.pojo.TbItem;
import com.yyl.pojo.TbItemExample;
import com.yyl.pojo.TbItemExample.Criteria;
/**
 * 商品查询service
 * @author yangyl
 *
 */
@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private TbItemMapper itemMapper;

	@Override
	public TbItem getItemById(long itemId) {
		// TODO Auto-generated method stub
		//TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//
		TbItemExample example = new TbItemExample();
		Criteria criteria = example.createCriteria(); 
		criteria.andIdEqualTo(itemId);
		List<TbItem> list = itemMapper.selectByExample(example);
		TbItem item = null;
		//判断List中是否为空
		if(list != null && list.size() > 0) {
			item = list.get(0);
		}
		return item;
	}

	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		//分页处理
		PageHelper.startPage(page, rows);
		// 执行查询
		TbItemExample example = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(example);
		//取分页信息
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		//返回处理结果
		EasyUIDataGridResult edg = new EasyUIDataGridResult();
		edg.setTotal(pageInfo.getTotal());
		edg.setRows(list);
		return edg;
	}

	@Override
	public TaotaoResult CreatItem(TbItem tbItem, String desc) {
		// 生成商品id
		long itemId = IDUtils.genItemId();
		//补全TbItem属性
		tbItem.setId(itemId);
		//商品状态，1-正常，2-下架，3-删除
		tbItem.setStatus((byte)1);
		//创建时间和更新时间
		tbItem.setCreated(new Date());
		tbItem.setUpdated(new Date());
		itemMapper.insert(tbItem);
		//商品描述
//		TbItemDesc tbItemDesc = new TbItemDesc();
//		tbItemDesc.setItemId(itemId);
//		tbItemDesc.setItemDesc(desc);
//		tbItemDesc.setCreated(new Date());
//		tbItemDesc.setUpdated(new Date());
//		itemDescMapper.insert(tbItemDesc);
		
		return TaotaoResult.ok();
	}

}
